package com.doradance.app.activity;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.doradance.R;
import com.doradance.account.utils.NetConnected;
import com.doradance.app.curriculum.CurriculumFragment;
import com.doradance.app.mine.MineFragment;
import com.doradance.entity.RefreActivityEntity;
import com.pachong.android.baseuicomponent.fragment.BaseFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * A simple {@link Fragment} subclass.
 */
public class ActivityHomeFragment extends BaseFragment {


    public ActivityHomeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        EventBus.getDefault().register(this);
        isConnected();
        return view;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(RefreActivityEntity refreshEvent){
        isConnected();
    }

    private void isConnected() {
        if(NetConnected.isConnected(getContext())==false){
            getChildFragmentManager().beginTransaction().replace(R.id.fl_activity_home, new NoNetFragment()).commit();
            return;
        }else{
            getChildFragmentManager().beginTransaction().replace(R.id.fl_activity_home, new CurriculumFragment()).commit();
        }
    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_activity_home, container, false);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }
}
